package org.xg.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName SencodTimeServlet
 * @date 2021/9/24 14:55
 **/
@WebServlet("/second")
public class SecondTimeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter = resp.getWriter();
        String value = (String) req.getSession().getAttribute("lastTime");
        printWriter.write("<h1>编程帮 www.baicnhegn.net</h1>"
                + "<h3>欢迎您的归来</h3><h3>您上次访问的时间是" + value + "</h3>");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(date);
        req.getSession().setAttribute("lastTime", sDate);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
